package me.shurufa.activities;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.shurufa.R;
import me.shurufa.fragments.SearchBookFragment;
import me.shurufa.utils.Constants;
import me.shurufa.utils.StatusBarCompat;
import me.shurufa.utils.Utils;

/**
 * 作者 : andy
 * 日期 : 16/3/14 14:19
 * 邮箱 : andyxialm@gmail.com
 * 描述 : 查书界面
 */
public class MainSearchBookActivity extends AppCompatActivity implements View.OnClickListener {//, TextWatcher, TextView.OnEditorActionListener {

    @Bind(R.id.right_text)
    TextView right_text;
    @Bind(R.id.fragment_container)
    FrameLayout fragmentContainer;

    @Bind(R.id.search_edit)
    EditText searchEdit;

    SearchBookFragment mSearchFragment;
    private Handler mHandler = new Handler();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        setContentView(R.layout.activity_search_book);
        StatusBarCompat.setColor(this, getResources().getColor(R.color.red_new), 0);
        ButterKnife.bind(this);
        uiInit();
        initLinstener();

        // 弹出键盘
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Utils.showSoftkeyboard(MainSearchBookActivity.this, searchEdit);
            }
        }, 400);
    }

    private void uiInit() {
        Utils.showSoftkeyboard(this, searchEdit);
        right_text.setText(getString(R.string.cancel));
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        mSearchFragment = SearchBookFragment.newInstance(Constants.FROM_MAIN_SEARCH, Constants.FROM_MAIN_SEARCH);
        ft.replace(R.id.fragment_container, mSearchFragment);
        ft.commit();

    }

    private void initLinstener() {
        right_text.setOnClickListener(this);

        // 编辑框回车搜索
        searchEdit.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    // 触发-搜索
                    search(searchEdit.getText().toString());
                    Utils.closeInputMethod(MainSearchBookActivity.this, searchEdit);
                    // 防止-焦点下移
                    return true;
                }

                return false;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.right_text:
                Utils.closeInputMethod(this, searchEdit);
                finish();
                break;
        }
    }

    public void search(String text) {

        if (TextUtils.isEmpty(text)) {
            fragmentContainer.setVisibility(View.GONE);
            return;
        }
        mSearchFragment.search(text, 0);
    }

}
